﻿// 系统中的表单基类
Ext.define('Kwan.ex.FormWin', {
    extend: 'Ext.window.Window',
    border: false,
    layout: 'fit',
    plain: true,
    saveParams: {},
    closeAction: 'destroy',
    maximizable: true,
    minimizable: true,
    closable: true,
    modal: true,
    iconCls: 'application_form',
    operType: '', // 操作类型,
    formWinItems: [], // 表单包含的字段
    formColumns: 2, // 有几列
    formWinLabelWidth: 70, // 字段label的宽度
    formWinFieldWidth: 200, // 字段的总宽度
    saveUrl: '', // 提交的url地址
    loadUrl: '', // 读取数据的Url地址
    fPanel: null, // 操作的父页面对象
    objId: null, // 要修改的数据的id
    disableFields: [], // 编辑时要禁用的字段['Text', 'Code']
    newData: {},
    getBaseForm: function () {
        return this.form.form;
    },
    initComponent: function () {
        var me = this;
        this.form = Ext.create('Ext.form.Panel', {
            id: 'fp_' + this.fpID,
            waitMsgTarget: true,
            autoScroll: true,
            bodyPadding: 10,
            title: "基本信息",
            border: false,
            defaults: { labelAlign: 'right', labelWidth: this.formWinLabelWidth, width: this.formWinFieldWidth },
            layout: { scope: this, type: 'table', columns: this.formColumns },
            items: this.formWinItems,
            listeners: {
                scope: this,
                beforerender: function () {
                    switch (this.operType) {
                        case "edit":
                            this.form.form.load({ scope: this, url: this.loadUrl, //waitMsg: '请稍后..',
                                params: { s_method: 'list', query1: 'EntityID=' + this.objId }
                            }); break;
                        case "add":
                            Kwan.bindForm(this.newData, this.form.form); break;
                    }
                }
            }
        });
        this.tab = Ext.create('Ext.tab.Panel', {
            items: [this.form]
        });

        this.items = [this.tab];
        switch (this.operType) {
            case 'add':
                if (this.title == null)
                    this.title = '新增';
                this.dockedItems = [{
                    xtype: 'toolbar',
                    dock: 'bottom',
                    ui: 'footer',
                    layout: { pack: 'center' },
                    items: [{
                        scale: 'medium', text: '提交', style: 'margin:1px,1px,1px,1px', icon: "/Admin/style/24Icons/clean.png",
                        width: 80, height: 35,
                        listeners: {
                            scope: this,
                            click: function () {
                                var form = this.form.form;
                                if (form.isValid()) {
                                    //var data = Ext.Object.toQueryString(this.form.getValues());
                                    Ext.Ajax.request({
                                        url: this.saveUrl,
                                        params: { s_method: 'save', data: Ext.encode(this.form.getValues()) },
                                        success: function (response) {
                                            var data = eval('(' + response.responseText + ')');
                                            if (data.success) {
                                                Ext.getCmp(me.fPanel).getStore().load();
                                                me.close();
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }, {
                        scale: 'medium',
                        text: '重置',
                        style: 'margin:1px,1px,1px,1px',
                        icon: "/Admin/style/24Icons/new-view-refresh.png",
                        width: 80,
                        height: 35,
                        listeners: {
                            scope: this,
                            click: function () {
                                this.form.form.reset();
                            }
                        }
                    }, {
                        scale: 'medium',
                        text: '退出',
                        style: 'margin:1px,1px,1px,1px',
                        icon: "/Admin/style/24Icons/Cancel.png",
                        width: 80,
                        height: 35,
                        listeners: {
                            scope: this,
                            click: function () {
                                this.close();
                            }
                        }
                    }]
                }];

                break;
            case 'edit':
                if (this.title == null)
                    this.title = '修改';
                this.dockedItems = [{
                    xtype: 'toolbar',
                    dock: 'bottom',
                    ui: 'footer',
                    layout: {
                        pack: 'center'
                    },
                    items: [{
                        scale: 'medium',
                        text: '提交',
                        style: 'margin:1px,1px,1px,1px',
                        icon: "/Admin/style/24Icons/clean.png",
                        width: 80,
                        height: 35,
                        listeners: {
                            scope: this,
                            click: function () {
                                var form = this.form.form;
                                if (form.isValid()) {
                                    //var data = Ext.Object.toQueryString(this.form.getValues());
                                    var data = Ext.encode(Ext.apply(this.form.getValues(), { EntityID: this.objId }));
                                    Ext.Ajax.request({
                                        url: this.saveUrl,
                                        params: { s_method: 'save', EntityID: this.objId, data: data },
                                        success: function (response) {
                                            var text = eval('(' + response.responseText + ')');
                                            if (text.success) {
                                                Ext.getCmp(me.fPanel).getStore().load();
                                                me.close();
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    }, {
                        scale: 'medium',
                        text: '重置',
                        style: 'margin:1px,1px,1px,1px',
                        icon: "/Admin/style/24Icons/new-view-refresh.png",
                        width: 80,
                        height: 35,
                        listeners: {
                            scope: this,
                            click: function () {

                                this.form.form.reset();
                            }
                        }
                    }, {
                        scale: 'medium',
                        text: '退出',
                        style: 'margin:1px,1px,1px,1px',
                        icon: "/Admin/style/24Icons/Cancel.png",
                        width: 80,
                        height: 35,
                        listeners: {
                            scope: this,
                            click: function () {
                                this.close();
                            }
                        }
                    }]
                }];
                break;
        }
        this.callParent(arguments);
    }
});